package jp.syuriken.snsw.terenaibot.format.function;

import jp.syuriken.snsw.terenaibot.format.CompileArg;
import jp.syuriken.snsw.terenaibot.format.FormatSyntaxException;
import jp.syuriken.snsw.terenaibot.format.PrecompiledSyntaxGroup;

/**
 * TODO snsoftware
 * 
 * @author $Author: snswinhaiku.lo@gmail.com $ */
public class IntMultiFunction extends AbstractFormatFunction {
	
	@Override
	public String getReturnValue(PrecompiledSyntaxGroup[] args, CompileArg compileArg) throws FormatSyntaxException {
		checkArgLength(args, compileArg, "*", 2, -1);
		int result = 0;
		try {
			result = Integer.parseInt(args[0].format(compileArg));
		} catch (NumberFormatException e) {
			throw new FormatSyntaxException(String.format("Function<*>: arg%d is not integer value: %s (formatted:%s)",
					0, args[0].toString(), args[0].format(compileArg)));
		}
		for (int i = 1; i < args.length; i++) {
			PrecompiledSyntaxGroup arg = args[i];
			try {
				result *= Integer.parseInt(arg.format(compileArg));
			} catch (NumberFormatException e) {
				throw new FormatSyntaxException(String.format(
						"Function<*>: arg%d is not integer value: %s (formatted:%s)", i, arg.toString(),
						arg.format(compileArg)));
			}
		}
		return String.valueOf(result);
	}
	
}
